package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.AuthenDateAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.AuthenDetailPagerAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.AuthenProjectAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.AuthenBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.MyCollectBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.BaseAdapter;

import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.ADDCOLLECT;
import static com.shixi.shixitraining.shixitraining.constant.Constants.TRAINDETAIL;

public class AuthenDetailActivity extends BaseActivity implements View.OnClickListener {

    private String collection;
    private String collectionType;
    private String id;
    private ImageView mCollectImg;
    private TextView mCollectTv;
    private  boolean isSelected;
    private String projectId;
    private String dateId;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authen_detail);
        initView();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String trainId = bundle.getString("trainId");
            requestJobDetail(trainId);
        }
    }

    private void requestJobDetail(String trainId) {

        HttpUtil.getInstance().getApiInterface().AuthenDetail(TRAINDETAIL + trainId).enqueue(new CallbackImple<AuthenBean>() {
            private String price;
            private  String teacherName;
            private String date;
            @Override
            public void onError(Call<AuthenBean> call, Throwable t) {
                ToastUtil.showShortToast(AuthenDetailActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<AuthenBean> call, AuthenBean response) {
                if (response != null) {
                    if (response.getData() != null) {
                        String imageUrl = response.getData().getImage();
                        String address = response.getData().getAddress();
                        collection = response.getData().getCollection();
                        collectionType = response.getData().getCollection_type();
                        id = response.getData().getId();
                        String title = response.getData().getTitle();

                        AuthenDetailPagerAdapter adapterTab = new AuthenDetailPagerAdapter(getSupportFragmentManager(),response);
                        mViewPager.setAdapter(adapterTab);

                        List<AuthenBean.DataBean.TeacherBean> teacher = response.getData().getTeacher();
                        for (int t = 0; t < teacher.size(); t++) {
                            AuthenBean.DataBean.TeacherBean teacherBean = teacher.get(t);
                          teacherName = teacherBean.getName();
                        }
                        List<AuthenBean.DataBean.SeriesBean> series = response.getData().getSeries();
                        List<AuthenBean.DataBean.CoursepriceBean> courseprice = response.getData().getCourseprice();
                        for (int i = 0; i < courseprice.size(); i++) {

                            AuthenBean.DataBean.CoursepriceBean coursepriceBean = courseprice.get(i);
                            price = coursepriceBean.getPrice();
                            date = coursepriceBean.getDate();

                        }
                        ImageView imageView = (ImageView) findViewById(R.id.authen_detail_img);
                        Glide.with(AuthenDetailActivity.this).load(imageUrl).placeholder(R.drawable.banner_default).into(imageView);
                        TextView city_tv = (TextView) findViewById(R.id.authen_detail_city);
                        TextView price_tv = (TextView) findViewById(R.id.authen_detail_price);
                        TextView title_tv = (TextView) findViewById(R.id.authen_session_title);
                        TextView teacher_tv = (TextView) findViewById(R.id.authen_teacher_tv);
                        title_tv.setText(title);
                        city_tv.setText(address);
                        price_tv.setText("￥"+price+"起/人");
                        teacher_tv.setText(teacherName);
                        //项目
                        final RecyclerView mRcviewProject = (RecyclerView) findViewById(R.id.recview_authen_project);
                        GridLayoutManager manager = new GridLayoutManager(AuthenDetailActivity.this, 1);
                        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                        mRcviewProject.setLayoutManager(manager);
                        AuthenProjectAdapter  adapter=new AuthenProjectAdapter(AuthenDetailActivity.this,series);
                        mRcviewProject.setAdapter(adapter);
                        adapter.setOnCustomItemClickListener(new BaseAdapter.OnCustomItemClickListener<AuthenBean.DataBean.SeriesBean>() {
                            @Override
                            public void onCustomItemClick(AuthenBean.DataBean.SeriesBean item, int position) {
                                projectId = item.getId();
                            }
                        });


                        //项目日期
                        RecyclerView mRcviewDate = (RecyclerView) findViewById(R.id.recview_authen_date);
                        GridLayoutManager managerDate = new GridLayoutManager(AuthenDetailActivity.this, 1);
                        managerDate.setOrientation(LinearLayoutManager.HORIZONTAL);
                        mRcviewDate.setLayoutManager(managerDate);
                        AuthenDateAdapter  adapterDate=new AuthenDateAdapter(AuthenDetailActivity.this,courseprice);
                        mRcviewDate.setAdapter(adapterDate);
                        adapterDate.setOnCustomItemClickListener(new BaseAdapter.OnCustomItemClickListener<AuthenBean.DataBean.CoursepriceBean>() {
                            @Override
                            public void onCustomItemClick(AuthenBean.DataBean.CoursepriceBean item, int position) {
                                dateId = item.getId();
                            }
                        });

                    } else {
                        ToastUtil.showShortToast(AuthenDetailActivity.this, response.getMsg());
                    }
                } else {
                    ToastUtil.showShortToast(AuthenDetailActivity.this, "数据为空");
                }

            }
        });

    }
    private void initView() {

        TextView mApplyTv = (TextView) findViewById(R.id.tv_apply_train);
        ImageView mBack = (ImageView) findViewById(R.id.iv_detail_back);
        ImageView mShare = (ImageView) findViewById(R.id.iv_detail_share);
        LinearLayout mCollectLin = (LinearLayout) findViewById(R.id.lin_collection_train);
        mCollectImg = (ImageView) findViewById(R.id.iv_collection_train);
        mCollectTv = (TextView) findViewById(R.id.tv_collection_train);
        mApplyTv.setOnClickListener(this);
        mBack.setOnClickListener(this);
        mShare.setOnClickListener(this);
        mCollectLin.setOnClickListener(this);

        TabLayout mTab = (TabLayout) findViewById(R.id.authen_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.authen_viewpager);
        mTab.setTabMode(TabLayout.MODE_FIXED);
        mTab.setupWithViewPager(mViewPager);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_apply_train:
                Bundle bundle=new Bundle();
                bundle.putString("train",projectId);
                bundle.putString("courseprice",dateId);
                Intent intent=new Intent(this, SessionApplyActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.iv_detail_back:
                this.finish();
                break;
            case R.id.iv_detail_share:
                break;
            case R.id.lin_collection_train:
                addTrainCollect(collectionType,id);
                break;
        }
    }

    private void addTrainCollect(String collectionType, String id) {
        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();
        String url=ADDCOLLECT+"&token="+token+"&userId="+userId+"&collection_type="+collectionType+"&id="+id;

        HttpUtil.getInstance().getApiInterface().myCollect(url).enqueue(new CallbackImple<MyCollectBean>() {
            @Override
            public void onError(Call<MyCollectBean> call, Throwable t) {
                ToastUtil.showShortToast(AuthenDetailActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<MyCollectBean> call, MyCollectBean response) {

                if (response.getStatus()==200){
                    ToastUtil.showShortToast(AuthenDetailActivity.this,response.getMsg());
                    if (collection.equals(0)){
                        mCollectImg.setImageResource(R.drawable.icon_collect);
                        mCollectTv.setText("收藏");
                    }else if (collection.equals(1)){
                        mCollectImg.setImageResource(R.drawable.icon_collect_selected);
                        mCollectTv.setText("已收藏");
                    }

                }else {
                    ToastUtil.showShortToast(AuthenDetailActivity.this,response.getMsg());
                }

            }
        });

    }
}
